Partial Public Class pages_EmailTemplates_default : Inherits CPage

#region "Querystring (Filters)"
    Public ReadOnly Property Search As String
        Get
            Return CWeb.RequestStr("search")
        End Get
    End Property
    
    'Rename or Delete:
    Public ReadOnly Property ShowComponents As Boolean
        Get
            Return CWeb.RequestBool("showComponents")
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [EmailTemplates]() As CEmailTemplateList
        Get
            Return CEmailTemplate.Cache.GetByHasSubject(Not ShowComponents).Search(txtSearch.Text)
        End Get
    End Property
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        'Populate Dropdowns
        rbl.Items(0).Text = CUtilities.NameAndCount(rbl.Items(0).Text, CEmailTemplate.Cache.GetByHasSubject(True))
        rbl.Items(1).Text = CUtilities.NameAndCount(rbl.Items(1).Text, CEmailTemplate.Cache.GetByHasSubject(False))

        'Search state (from querystring)
        txtSearch.Text = Me.Search
        rbl.SelectedIndex = IIf(ShowComponents, 1, 0)
        
        'Display Results
        ctrlEmailTemplates.Display(Me.EmailTemplates, Not ShowComponents)

        'Client-side
        Me.Form.DefaultFocus  = txtSearch.ClientID  'txtCreate.ClientID
        Me.Form.DefaultButton = btnSearch.UniqueID  'CTextbox.OnReturnPress(txtSearch, btnSearch)
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click, rbl.SelectedIndexChanged
        Response.Redirect(CSitemap.EmailTemplates(txtSearch.Text, rbl.SelectedIndex = 1))
    End Sub
#End Region

#Region "Event Handlers - UserControl"
    Private Sub ctrl_AddClick() Handles ctrlEmailTemplates.AddClick
        Response.Redirect(CSitemap.EmailTemplateAdd())
    End Sub
    Private Sub ctrl_ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer) Handles ctrlEmailTemplates.ResortClick
        Response.Redirect(CSitemap.EmailTemplates(txtSearch.Text, ShowComponents, New CPagingInfo(0, pageNumber - 1, sortBy, descending)))
    End Sub
#End Region

End Class
